﻿using System.Windows;
using System.Windows.Controls;
using System;

namespace PilotOS.Controls
{
    public partial class PlaneControl : UserControl
    {
        public PlaneControl()
        {
            InitializeComponent();

            this.Loaded += (s, e) =>
                {
                    UpdateAltitude();
                };
        }

        private void UpdateAltitude()
        {
            if (_planeShadow != null)
            {
                Thickness t = new Thickness(this.Altitude, this.Altitude, -this.Altitude, -this.Altitude);
                _planeShadow.Margin = t;

                if (this.Altitude <= 1)
                {
                    if (Landed != null)
                    {
                        Landed(this, EventArgs.Empty);
                    }
                }
            }
        }

        public event EventHandler Landed;

        public double Altitude
        {
            get { return (double)GetValue(AltitudeProperty); }
            set { SetValue(AltitudeProperty, value); }
        }

        public static readonly DependencyProperty AltitudeProperty =
            DependencyProperty.Register(
                "Altitude", 
                typeof(double), 
                typeof(PlaneControl), 
                new PropertyMetadata(200.0, OnAltitudeChanged));

        private static void OnAltitudeChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            PlaneControl pc = sender as PlaneControl;
            pc.UpdateAltitude();
        }
    }
}
